<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div class="form">
            <div class="item">
                <label for="">用户名</label>
                <input type="text" class="user" v-model="userInfo.user">
            </div>
            <div class="item">
                <label for="">密码</label>
                <input type="password" class="pwd" v-model="userInfo.pwd">
            </div>
            <button class="login" @click="login()">登录</button>
            <button class="info" @click="getInfo()">获取信息</button>

        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        axios.interceptors.request.use(
            config => {
                const token = window.localStorage.token;
                if (token) {
                    config.headers.common["Authorization"] = "Bearer " + token
                }
                return config
            },
            err => {
                return Promise.reject(err)
            }

        )

        // axios.interceptors.response.use(
        //     response => {
        //         app.logs.push(JSON.stringify(response.data));
        //         return response;
        //     },
        //     err => {
        //         app.logs.push(JSON.stringify(response.data));
        //         return Promise.reject(err);
        //     }
        // );
        var app = new Vue({
            el: '#app',
            data: {
                message: 'hello world',
                userInfo: {
                    user: '',
                    pwd: ''
                }

            },
            methods: {
                login() {

                    axios.post('http://localhost:3000/login', this.userInfo).then(res => {
                        // console.log(res);
                        localStorage.setItem('token', res.data.token)
                    })

                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                },
                getInfo() {
                    axios.post('http://localhost:3000/getuser',this.userInfo).then();
                }
            }
        })
    </script>
</body>

</html>